<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

session_start();
function checkLogin()
{
	$url_login =  'c_login'; 
	$domain = $_SERVER["SERVER_NAME"];
	$url = $_SERVER["REQUEST_URI"];
	$segment = explode('c=',$url);
	$controller =  @$segment[count($segment)-1];
	$segment = explode('&',$controller);
	$controller =  @$segment[0];
	$index = false;
	if ("http://".$domain.$url == base_url() || "https://".$domain.$url == base_url()
		|| "http://".$domain.$url == base_url()."/index.php" || "http://".$domain.$url == base_url()."/index.php"){
		$index = true;
	}
	
	if (@$_SESSION[SESSION_LOGIN] == 1){
		if ($index){
			redirect(base_url().'index.php?c=main');
		}else{
			if ($controller == $url_login){
				if (@$segment[2] != 'logout')
					redirect(base_url().'index.php?c=main');
			}
		}
	}else{
		if ($controller != $url_login)
			redirect(base_url().'index.php?c=c_login');
	}
}

function setLogin($data){
	$_SESSION[SESSION_LOGIN] = 1;
	$_SESSION[SESSION_USERID] = $data[SESSION_USERID];
	$_SESSION[SESSION_GROUPID] = $data[SESSION_GROUPID];
	$_SESSION[SESSION_GROUPNAMA] = $data[SESSION_GROUPNAMA];
}

function setLogout(){
	$_SESSION[SESSION_LOGIN] = 0;
	session_destroy();
	redirect(base_url());
}